import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import subjects from '@/components/subjects'
import teacher from '@/components/teacher'
import login from '@/components/login'
import store from "../store/store";

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/subjects',  // http://localhost:8080/subjects
      name: 'subjects',
      component: subjects,
      // meta:{
      //   requireAuth: false  //不需要鉴权
      // }
    },
    {
      path: '/login',
      name: 'login',
      component: login,
      meta:{
        requireAuth: false  //不需要鉴权
      }
    },
    {
      path: '/teachers/:sno/:sname?',
      name: 'teacher',
      component: teacher
      // meta:{
      //   requireAuth: true  //不需要鉴权
      // }
    }
  ],
  mode:'history'  // 路由中间默认带有#，如果需要去掉#，只需将mode的默认值’hash’改为’history’即可
})

router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => record.meta.requireAuth)
  const token = localStorage.getItem("token")

  console.log("requiresAuth:",requiresAuth)
  console.log("token:",token)

  if (requiresAuth && !token) {
    console.log("进入登录流程")
    next('/login')
  } else {
    console.log("进入正常流程")
    console.log("token:",token)
    next()
  }
})

export default router

